using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TheFugitive
{


    class Car
    {
        public Level Level
        {
            get { return level; }
        }
        Level level;

        /// <summary>
        /// Position in world space of the bottom center of this enemy.
        /// </summary>
        public Vector2 Position
        {
            get { return position; }
        }
        Vector2 position;

        private Animation idleAnimation;
        private AnimationPlayer sprite;

        int direction;

        public bool isReady;
        public int moveSpeed;
        private Rectangle localBounds;
        /// <summary>
        /// Gets a rectangle which bounds this enemy in world space.
        /// </summary>
        public Rectangle BoundingRectangle
        {
            get
            {
                int left = (int)Math.Round(Position.X - sprite.Origin.X) + localBounds.X;
                int top = (int)Math.Round(Position.Y - sprite.Origin.Y) + localBounds.Y;

                return new Rectangle(left, top, localBounds.Width, localBounds.Height);
            }
        }

        public Car(Level level, Vector2 position, int direction)
        {
            this.level = level;
            this.position = position;
            this.direction = direction;
            //Random r = new Random(100);
            //direction = r.Next(0, 1);   // 0 : left  1: right

            //if (direction == 0)
            //{
            //    position.X = 400;
            //}
            //else
            //    position.X = 800;

            Random r = new Random(DateTime.Now.Millisecond);
            moveSpeed = r.Next(3, 20);

            LoadContent();
        }

        public void LoadContent()
        {
            // Load animations.
            idleAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Obstacle/Car"), 0.15f, true);
            //sprite.PlayAnimation(idleAnimation);

            // Calculate bounds within texture size.
            int width = (int)(idleAnimation.FrameWidth * 0.35);
            int left = (idleAnimation.FrameWidth - width) / 2;
            int height = (int)(idleAnimation.FrameWidth * 0.7);
            int top = idleAnimation.FrameHeight - height;
            localBounds = new Rectangle(left, top, width, height);

            sprite = new AnimationPlayer();
        }

        public void Update(GameTime gameTime)
        {
            if (isReady)
            {
                Random r = new Random(DateTime.Now.Millisecond);
                int random = r.Next(0, 50);
                if (random == 0)
                {
                    //Random r1 = new Random(DateTime.Now.Millisecond);
                    moveSpeed = r.Next(3, 20);
                    position.X = 800;
                    direction = random;
                    isReady = false;
                }
                else if (random == 1)
                {
                    //Random r1 = new Random(DateTime.Now.Millisecond);
                    moveSpeed = r.Next(3, 20);
                    position.X = 0;
                    direction = 1;
                    isReady = false;
                }

                return;
            }
            if (direction == 0)
                position.X -= moveSpeed;
            else
                position.X += moveSpeed;

            if (position.X < 0 || position.X > 800)
                isReady = true;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (isReady)
                return;
            sprite.PlayAnimation(idleAnimation);
            SpriteEffects flip = direction > 0 ? SpriteEffects.None : SpriteEffects.FlipHorizontally;
            sprite.Draw(gameTime, spriteBatch, Position, flip);
        }
    }
}
